package cn.HL.user.service.impl;

import cn.HL.basic.constants.BaseConstant;
import cn.HL.basic.dto.RegisterDto;
import cn.HL.basic.exception.BusinessException;
import cn.HL.basic.util.MD5Utils;
import cn.HL.basic.util.StrUtils;
import cn.HL.user.domain.Logininfo;
import cn.HL.user.domain.User;
import cn.HL.user.mapper.LogininfoMapper;
import cn.HL.user.mapper.UserMapper;
import cn.HL.user.service.IUserService;
import cn.HL.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HL
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    /**
     * 注册
     * @param dto
     */
    @Override
    public void registerPhone(RegisterDto dto) {
        //1接收所有的参数
        //手机号
        String phone = dto.getPhone();
        //密码
        String password = dto.getPassword();
        //第二次输入的密码
        String passwordRepeat = dto.getPasswordRepeat();
        //手机验证码
        String smsCode = dto.getSmsCode();
        //空校验
        if(StringUtils.isEmpty(phone)
                ||StringUtils.isEmpty(password)
                ||StringUtils.isEmpty(passwordRepeat)
                ||StringUtils.isEmpty(smsCode)){
            throw  new BusinessException("参数不能为空哦");
        }
        //校验两次密码是否正确
        if(!password.equals(passwordRepeat)){
            throw  new BusinessException("密码不一致,请重新填写");
        }
        //校验一下手机是否被注册过
        if(userMapper.loadByPhone(phone)!=null){
            throw  new BusinessException("已经注册过,请找回密码");
        }
        //验证码校验
        //先用手机号通过redis获取一下看能不能获取得到
        String key=phone+ BaseConstant.RegisterConstant.REGISTER_SMS_PREFIX;
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);
        if(smsCodeTmp==null){
            throw  new BusinessException("验证码已过期,请重新获取");
        }
        //比较验证码是否一致
        if(!smsCode.equalsIgnoreCase(smsCodeTmp.toString().split(":")[0])){
            throw  new BusinessException("验证码不正确请重新填写");
        }
        //一致了就需要新增了
        //先新增LoginInfo 要获取到的自增id
        User user = dto2User(dto);
        Logininfo logininfo =user2Logininfo(user);
        //新增logininfo
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        //新增user
        userMapper.save(user);
    }

    @Override
    public User loadByLogininfoId(Long id) {
        return userMapper.loadByLogininfoId(id);
    }

    /**
     * 联级修改
     * @param user
     */
    @Override
    public void update(User user) {
        Logininfo logininfo =user2Logininfo(user);
        user.setLogininfoId(logininfo.getId());
        userMapper.update(user);
        logininfoMapper.update(logininfo);
    }

    /**
     * 联级修改
     * @param id
     */
    @Override
    public void removeById(Long id) {
        User user = userMapper.loadById(id);
        logininfoMapper.deleteById(user.getLogininfoId());
        userMapper.deleteById(id);
    }


    /**
     * 数据的拷贝
     * @param user
     * @return
     */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //拷贝数据只要两个对象里面的有的属性名一样就能拷贝
        BeanUtils.copyProperties(user,logininfo);
        //设置为用户
        logininfo.setType(1);
        return logininfo;
    }
    /**
     * 数据的拷贝
     * @return
     */
    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setPhone(dto.getPhone());
        user.setUsername(dto.getPhone());
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        //设置面密码
        String password = MD5Utils.encrypByMd5(dto.getPassword()+salt);
        user.setPassword(password);
        return user;

    }
}
